<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/ss.jpg" alt="" />
      </div>

      <!-- 登陆表单 -->
      <el-form
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="loginForm"
        label-width="0px"
        class="login_form"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-s-goods"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            @click="login"
            v-loading.fullscreen.lock="fullscreenLoading"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../api/interface";
import { mapMutations } from "vuex";
import router from "../router/index";
import QS from "qs";
export default {
  data() {
    return {
      // 登陆表单数据绑定
      loginForm: {
        //带着默认值，为了方便，不用每次都输入
        username: "account",
        password: "password",
      },
      //加载
      fullscreenLoading: false,
      // 表单验证规则属性
      loginFormRules: {
        // 账号校验
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度为 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        // 密码校验
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 8,
            max: 16,
            message: "长度为 8 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    //重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 登录预验证
    ...mapMutations(["setToken"]),
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        //async await 就是解构 promise 数据的吧
        const result = await login(QS.stringify(this.loginForm));
        //获取到后端返回的信息
        console.log(result);

        //后端信息里加一个 code 状态码，根据这个状态码来判断登录情况
        if (result.code == 200) {
          //把token存入localStorage中，这个是长久存在的，不删除就不会消失
          //也可以把它存入sessionStorage中，这个应该是关闭就没了
          this.setToken({ token: result.data });
          //全局加载效果
          this.fullscreenLoading = true;
          setTimeout(() => {
            this.$message.success("登录成功!");
            this.fullscreenLoading = false;
            //路由跳转，3.0写法吧，2.0写法不行了
            router.push("/Home");
          }, 2000);
        }
        //这里应该直接一个else比较好
        else{
          this.fullscreenLoading = true;
          setTimeout(() => {
            this.$message.warning("登陆异常!");
            this.fullscreenLoading = false;
          }, 2000);
          
          router.push("/Login");
        }
      });
    },
  },

  created() {},
};
</script>

<style lang="less" scoped>
.login_container {
  background-image: url("../assets/bg1.jpg");
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    padding: 10px;
    // box-shadow: 0 0 3px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0%;
  width: 100%;
  padding: 0px 40px;
  box-sizing: border-box;
}
</style>
